package edu.ciee.tz.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import edu.ciee.tz.model.Buyer;

@Component("buyerDao")
public class BuyerDAO {

	public List<Buyer> search(Buyer b) throws Exception {

		String query = "where 1=1";

		if (b.getId() != 0) 
			query = query + " and id like '%" + b.getId() + "%'";// this.getID()
		if (b.getName() != null) 
			query = query + " and name like '%" + b.getName() + "%'"; // this.getName()
		if (b.getTele() != null) 
			query = query + " and tele like '%" + b.getTele() + "%'"; // this.getName()
		
		@SuppressWarnings("unchecked")
		List<Buyer> buyer_list = hibernateTemplate.find("from Buyer u "+ query);

		return buyer_list;
	}

	public void insert(Buyer l) throws Exception {
		this.getHibernateTemplate().save(l);
	}

	public void delete(Buyer l) throws Exception {
		this.getHibernateTemplate().delete(l);

	}

	public void update(Buyer l) throws Exception {
		this.getHibernateTemplate().update(l);
	}

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
